<?php
/**
 * 模块:管理员角色管理
 * User: 文丹
 * Date: 2018/1/24
 * Time: 10:50
 */
namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;
class Role extends Power{

    /**
     *  角色 - 列表页面
     */
    public function index(){
        $roles = model('Role')->getRoles();
       /* echo "<pre>";
        print_r($roles);
        die;*/
        $superAdmin = Db::name('role')->where('id',1)->find();
        $count = model('Role')->count();
        $this->assign([
            'roles'=>$roles,
            'superAdmin'=>$superAdmin,
            'count'=>$count,
            'empty'=>'<tr class="text-c va-m empty"><td colspan="7">暂无数据</td></tr>'
        ]);

        return $this->fetch('role:index');
    }
    /**
     *  角色 - 搜索
     */
    public function search(){

        $role_name = trim(input('role_name'));
      //  dump($role_name);die;
        if(empty($role_name)){
            return 1;
        }
        $result = model('Role')->roleNameSearch($role_name);
        $count  = model('Role')->roleNameCount($role_name);
      // print_r($result);die;
        if(!empty($result[0])){
            $this->assign('roles',$result);
            $this->assign('count',$count);
            return $this->fetch('role:index',['role_name'=>$role_name]);
        }else{
            return 2;
        }

    }

    /**
     *  角色 - 添加页面
     */
    public function roleAdd(){

        $menus = model('menu')->getMenus();
        $result= array();
        foreach ($menus as $key => $v) {
            $result[$v['item_name']][]= $v;
        }
        /*echo "<pre>";
        print_r($result);
        echo "</pre>";*/
        $this->assign('result',$result);
        $this->assign('menus',$menus);
        return $this->fetch('role:role-add');
    }
    /**
     *  角色添加保存
     */
    public function roleSave(){

        $name      = trim(input('name'));
        $explain   = trim(input('explain'));
        $all= input('all/s');

        $arr = array_filter(explode(';',$all));

        /*print_r($arr);
        die;*/
        $arr1 = array();
        foreach($arr as $v){
            $arr1[] = explode(':',$v);
        }
        $arr2 = array();
        foreach($arr1 as $key=>$v){
            $arr2[$key]['item_id'] = $v[0];
            $arr2[$key]['menu_id'] = $v[1];
        }
        $menu_config = json_encode($arr2);
       /* echo $menu_config;
        print_r($arr2);
        die;*/

        if(empty($name) || empty($explain)|| empty($all)){
            return ['msg'=>'请填写完整的数据','ok'=>false];
        }
        if(!empty($name) && !empty($explain) && !empty($all)){
            $data   = ['name'=>$name,'explain'=>$explain,'menu_config'=>$menu_config];
            $result = model('Role')->roleSave($data);
            if($result){
                return ['msg'=>'增加成功！','ok'=>true];
            }else{
                return ['msg'=>'增加失败！','ok'=>false];
            }

        }
    }


    /**
     *  角色 - 修改（页面）
     */
    public function roleEdit(){
        $id      = intval(input('get.id'));
        $role    = model('Role')->getRole($id);
        $menu_config = json_decode($role['menu_config'],true);
        $now_menu = array(); //当前用户的菜单ID
        if($menu_config){
            foreach($menu_config as $k1=>$v1){
                if(isset($v1['menu_id'])){
                    $now_menu[] = explode(',',$v1['menu_id'],-1);
                }

            }
        }

        $menus = model('menu')->getMenus();
        $result= array();
        foreach ($menus as $key => $v) {
            $result[$v['item_name']][]= $v;
        }

        $this->assign('menus',$menus);
        $this->assign('now_menu',$now_menu);
        $this->assign('role',$role);
        $this->assign('result',$result);
       return $this->fetch('role:role-edit');
    }
    public function roleUpdate(){

        $role_id = intval(input('role_id'));
        $name      = trim(input('name'));
        $explain   = trim(input('explain'));
        $all= input('all/s');

        $arr = array_filter(explode(';',$all));

        /*print_r($arr);
        die;*/
        $arr1 = array();
        foreach($arr as $v){
            $arr1[] = explode(':',$v);
        }
        $arr2 = array();
        foreach($arr1 as $key=>$v){
            $arr2[$key]['item_id'] = $v[0];
            $arr2[$key]['menu_id'] = $v[1];
        }
        $menu_config = json_encode($arr2);
        if(empty($name) || empty($explain)|| empty($all)){
            return ['msg'=>'请填写完整的数据','ok'=>false];
        }
        if(!empty($name) && !empty($explain) && !empty($all)){
            $data   = ['name'=>$name,'explain'=>$explain,'menu_config'=>$menu_config];
            $result = model('Role')->roleUpdate($data,$role_id);
            if($result){
                return ['msg'=>'修改成功！','ok'=>true];
            }else{
                return ['msg'=>'修改失败！','ok'=>false];
            }

        }

    }
    /**
     *  角色 - 停用
     */
    public function roleStop(){

        $id  = intval(input('id'));
        $row = model('Role')->roleStop($id);
        if($row>0){
            return 1;
        }else{
            return 2;
        }

    }
    /**
     *  角色 - 启用
     */
    public function roleStart(){

        $id  = intval(input('id'));
        $row = model('Role')->roleStart($id);
        if($row>0){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     *  角色 - 删除
     */
    public function roleDel(){

        $id  = intval(input('id'));
        $row = model('Role')->roleDel($id);
        if($row>0){
            return 1;
        }else{
            return 2;
        }
    }


}
